<script setup>
import {defineProps, ref, defineEmits} from 'vue'
import {useRouter} from 'vue-router'

const router = useRouter()
import {userStore} from '@/stores/user.js'
import api from "@/api/index.js"
import {keyword, keywordItem} from '@/hooks/hot.js'
import {getIsImplantApp} from "@/utils/implant.js"


const emit = defineEmits(['changeCity', 'search'])

const user = userStore()

const props = defineProps({
  goSearch: Boolean,
  showPlaceholder: Boolean,
  showIcon: Boolean,
  placeholder: String
})
const actions = ref([])
const _getAddressDistrictTree = () => {
  let city = sessionStorage.getItem('city')
  let cityList = sessionStorage.getItem('cityList')
  if (city && cityList) {
    user.setCity(JSON.parse(city))
    actions.value = JSON.parse(cityList)
  } else {
    api.getAddressDistrictTree({
      level: 2
    }).then((res) => {
      // 模拟社区数据
      res.data = [{
        "id": "1668633566336126977",
        "label": "金象花园社区",
        "code": "5101",
        "pid": "1668633566336126976",
        "type": null,
        "businessType": null,
        "button": null,
        "createTime": null,
        "children": [],
        "name": "成都"
      }, {
        "id": "1668633565409185793",
        "label": "晨辉社区",
        "code": "5001",
        "pid": "1668633565409185792",
        "type": null,
        "businessType": null,
        "button": null,
        "createTime": null,
        "children": [],
        "name": "重庆"
      }, {
        "id": "1668633566382264349",
        "label": "锦华社区",
        "code": "5106",
        "pid": "1668633566336126976",
        "type": null,
        "businessType": null,
        "button": null,
        "createTime": null,
        "children": [],
        "name": "德阳"
      },]
      actions.value = res.data
      actions.value = res.data
      actions.value.forEach((item, index) => {
        item.name = item.label.replace(/市/g, '')
        if (index === 0) {
          user.setCity(item)
        }
      })
      sessionStorage.setItem('cityList', JSON.stringify(actions.value))
    })
  }
}
_getAddressDistrictTree()
const searchInputFocus = () => {
  if (props.goSearch) {
    router.push('/search')
  }
}
const show = ref(false)

const onSelect = (item) => {
  if (item.name !== user.city?.name) {
    user.setCity(item)
    emit('changeCity')
  }
  show.value = false
}
const clickOverlay = () => {
  show.value = false
}
const locationClick = () => {
  show.value = true
}

const key = ref('')
const searchClick = () => {
  if (props.showPlaceholder) {
    if (keywordItem.value.kindId === 0) {
      router.push(`/scenicspot/${keywordItem.value.businessId}`)
    } else if (keywordItem.value.kindId === 2) {
      router.push(`/hotel-detail/${keywordItem.value.businessId}`)
    } else if (keywordItem.value.kindId === 3) {
      router.push(`/hotpot/${keywordItem.value.businessId}`)
    }
  } else {
    emit('search', key.value)
  }
}

const getPlaceholder = () => {
  if (props.placeholder) {
    return props.placeholder
  }
  if (props.showPlaceholder) {
    return keyword.value
  }
  return ''
}

const goHomeClick = () => {
  window.location.href = 'http://close.com'
}
</script>

<template>
  <div class="header">
    <div v-if="getIsImplantApp()" class="home_icon" @click="goHomeClick">
      <van-icon size="28" name="wap-home-o" color="#FFFFFF"/>
    </div>
    <div class="location" @click="locationClick">
      <img src="../../assets/images/icons/location_FFFEF8.png" alt="">
      <span>{{ user.city?.name || '金象花园' }}</span>
    </div>
    <div class="input_box">
      <img v-if="showIcon" class="input_icon" src="@/assets/images/icons/search_icon.png" alt="">
      <input
          v-model="key"
          class="input"
          @focus="searchInputFocus"
          @click-overlay="clickOverlay"
          :placeholder="getPlaceholder()"
      />
      <div class="search_button" @click="searchClick">搜索</div>
    </div>
    <van-action-sheet v-if="show" v-model:show="show" :actions="actions" title="切换社区" @select="onSelect"/>
  </div>
</template>

<style scoped lang="scss">
.header {
  display: flex;
  padding: 0;
  font-size: 1.4rem;

  .home_icon {
    margin-right: .8rem;
  }

  .location {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 3.5rem;
    //width: 6.5rem;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 2rem;
    flex-shrink: 0;
    padding-right: 10px;
    padding-left: 10px;
    color: #FFFEF8;

    img {
      width: 1.6rem;
      height: 1.6rem;
      margin-right: .02rem;
    }

    span {
      padding: 0 .02rem;
    }
  }

  .input_box {
    flex: 1;
    display: flex;
    align-items: center;
    height: 3.5rem;
    margin-left: 1rem;
    border-radius: 3rem;
    border: 1px solid var(--van-primary-color);
    padding-left: 1.2rem;
    background: #FFFFFC;

    .input_icon {
      width: 1.47rem;
      height: 1.47rem;
    }

    .input {
      width: calc(100% - 6rem);
      border: none;
      outline: none;
      box-sizing: border-box;
      background-color: transparent;
      //color: #BBBBBB;
    }

    .search_button {
      height: 3rem;
      background-color: var(--van-primary-color);
      width: 7rem;
      border-radius: 3rem;
      line-height: 3rem;
      margin-right: .3rem;
      text-align: center;
      color: #ffffff;
      padding-left: .4rem;
    }

    .search_button:active {
      background-color: rgb(41 142 142);
    }
  }
}
</style>